package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.NcgAvoid;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.health.service.INcgAvoidService;
import com.sqx.modules.health.service.IUserService;
import com.sqx.modules.health.utils.MyBeanUtil;
import com.sqx.modules.health.vo.NcgAvoidVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用药禁忌表 前端控制器
 *
 * @author gxy
 * @since 2022-04-13
 */

@Api(value = "用药禁忌接口", tags = "用药禁忌接口", description = "")
@RestController
@RequestMapping("/health/ncgAvoid")
public class NcgAvoidController {
    @Autowired
    private INcgAvoidService ncgAvoidService;

    @Autowired
    private IUserService userService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新用药禁忌表", notes = "新增或更新用药禁忌表")
    public Result save(@RequestBody NcgAvoid ncgAvoid){
        if (ObjectUtil.isEmpty(ncgAvoid.getId())){
            ncgAvoid.setCreatetime(LocalDateTime.now());
        } else {
            ncgAvoid.setUpdatetime(LocalDateTime.now());
        }
        return Result.success(ncgAvoidService.saveOrUpdate(ncgAvoid));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除用药禁忌表", notes = "根据ID删除用药禁忌表")
    public Result delete(@PathVariable Integer id){
        return Result.success(ncgAvoidService.removeById(id));
    }

    // 根据IDS删除
    @GetMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除用药禁忌表", notes = "根据IDS删除用药禁忌表")
    public Result deleteBatch(@RequestParam List<Integer> ids){
        return Result.success(ncgAvoidService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部用药禁忌表", notes = "查询全部用药禁忌表")
    public Result findAll(){
        return Result.success(ncgAvoidService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录用药禁忌表", notes = "根据ID查询一条记录用药禁忌表")
    public Result findOne(@RequestParam Integer id){
        return Result.success(ncgAvoidService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询用药禁忌表", notes = "分页查询用药禁忌表")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String name){
        QueryWrapper<NcgAvoid> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        Page<NcgAvoid> page = ncgAvoidService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<NcgAvoidVo> ncgServiceOrderVos = MyBeanUtil.collectionCopy(page.getRecords(), NcgAvoidVo.class);
        Page<NcgAvoidVo> result = new Page<>();
        result.setRecords(ncgServiceOrderVos);
        result.setTotal(page.getTotal());
        for (NcgAvoidVo vo : result.getRecords()){
            if (ObjectUtil.isNotEmpty(vo.getUid())){
                SysUserEntity user = userService.getRedisByUid(vo.getUid());
                if (!ObjectUtil.isEmpty(user)) {
                    vo.setUname(ObjectUtil.isEmpty(user.getNickName()) ? user.getUsername() : user.getNickName());
                }
            }
        }
        return  Result.success(result);
    }

    @GetMapping("/images")
    public Result getFile(@RequestParam String fileName){
        System.out.println(fileName);
        String s = System.getProperty("user.dir") + "/src/main/resources/static".replaceAll("/", "\\");
        fileName = "file://" + s + fileName;
        return Result.success(fileName);
    }


}

